<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">名称</label>
        <el-input
          v-model="query.name"
          clearable
          placeholder="名称"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">实验室</label>
        <el-select
          v-model="query.labId"
          clearable
          style="width: 185px"
          placeholder="实验室"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option
            v-for="item in labs"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <label class="el-form-item-label">传感器类型</label>
        <el-select
          v-model="query.type"
          clearable
          style="width: 185px"
          class="filter-item"
          placeholder="传感器类型"
        >
          <el-option value="1" label="温度" />
          <el-option value="2" label="湿度" />
          <el-option value="3" label="压差" />
          <el-option value="4" label="氧气" />
          <el-option value="5" label="VOC" />
          <el-option value="6" label="乙炔" />
          <el-option value="7" label="氢" />
        </el-select>
        
        <!-- <label class="el-form-item-label">是否禁用短信报警</label>
        <el-input v-model="query.isForbidden" clearable placeholder="是否禁用短信报警" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">阈值</label>
        <el-input v-model="query.thresholdValue" clearable placeholder="阈值" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="550px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="100px"
        >
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" style="width: 370px" />
          </el-form-item>
          <el-form-item label="实验室" prop="labId">
            <el-select
              v-model="form.labId"
              style="width: 370px"
              clearable
              filterable
              :remote-method="getLabs"
              placeholder="请选择实验室"
            >
              <el-option
                v-for="item in labs"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="传感器类型" prop="type">
            <!-- <el-input v-model="form.type" style="width: 370px;" /> -->
            <el-select
              v-model="form.type"
              style="width: 370px"
              placeholder="请选择传感器类型"
            >
              <el-option label="温度" :value="1" />
              <el-option label="湿度" :value="2" />
              <el-option label="压差" :value="3" />
              <el-option :value="4" label="氧气" />
              <el-option :value="5" label="VOC" />
              <el-option :value="6" label="乙炔" />
              <el-option :value="7" label="氢" />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="实验室" prop="labId">
            <el-input v-model="form.labId" style="width: 370px;" />
          </el-form-item> -->
          <el-form-item label="ip" prop="ip">
            <el-input v-model="form.ip" style="width: 370px" />
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="form.address" style="width: 370px" />
          </el-form-item>
          <!-- <el-form-item label="从站地址" prop="slaveStationId">
            <el-input v-model="form.slaveStationId" style="width: 370px;" />
          </el-form-item> -->
          <!-- <el-form-item label="是否禁用短信报警">
            未设置字典，请手动设置 Radio
          </el-form-item>
          <el-form-item label="阈值">
            <el-input v-model="form.thresholdValue" style="width: 370px;" />
          </el-form-item> -->
          <el-form-item
            v-if="form.type == 1"
            label="温度阈值"
            prop="thresholdValue"
            style="width: 470px"
          >
            <template>
              <div class="block">
                <el-slider
                  v-model="form.thresholdValue"
                  :marks="marks2"
                  show-input
                />
              </div>
            </template>
          </el-form-item>
          <el-form-item
            v-if="form.type == 2"
            label="湿度阈值"
            prop="thresholdValue"
          >
            <template>
              <div class="block">
                <el-slider
                  v-model="form.thresholdValue"
                  :marks="marks"
                  show-input
                />
              </div>
            </template>
          </el-form-item>
          <el-form-item
            v-if="form.type == 3"
            label="压差阈值"
            prop="thresholdValue"
          >
            <template>
              <div class="block">
                <el-slider
                  v-model="form.thresholdValue"
                  :marks="marks3"
                  :max="50"
                  show-input
                />
              </div>
            </template>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
            >确认</el-button
          >
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="type" label="传感器类型">
          <template slot-scope="scope">
            <span v-if="scope.row.type == 1">温度</span>
            <span v-if="scope.row.type == 2">湿度</span>
            <span v-if="scope.row.type == 3">压差</span>
            <span v-if="scope.row.type == 4">氧</span>
            <span v-if="scope.row.type == 5">VOC</span>
            <span v-if="scope.row.type == 6">乙炔</span>
            <span v-if="scope.row.type == 7">氢</span>
          </template>
        </el-table-column>
        <el-table-column prop="labName" label="实验室" />
        <!-- <el-table-column prop="isForbidden" label="是否禁用短信报警" /> -->
        <el-table-column prop="thresholdValue" label="阈值">
          <template slot-scope="scope">
            <span v-if="scope.row.type == 1"
              >{{ scope.row.thresholdValue }}°C</span
            >
            <span v-if="scope.row.type == 2"
              >{{ scope.row.thresholdValue }}%</span
            >
            <span v-if="scope.row.type == 3"
              >{{ scope.row.thresholdValue }}Pa</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="data" label="传感器实时数据">
          <template slot-scope="scope">
            <span v-if="scope.row.type == 1 && scope.row.data">
              {{ scope.row.data }}°C
            </span>
            <span v-if="scope.row.type == 2 && scope.row.data">
              {{ scope.row.data }}%
            </span>
            <span v-if="scope.row.type == 3 && scope.row.data">
              {{ scope.row.data }}Pa
            </span>
            <span v-if="scope.row.data == null">
              - -
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="ip" label="ip" />
        <el-table-column prop="address" label="地址" />
        <el-table-column
          v-if="checkPer(['admin', 'sensor:edit', 'sensor:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSensor from "@/api/sensor";
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import rrOperation from "@crud/RR.operation";
import crudOperation from "@crud/CRUD.operation";
import udOperation from "@crud/UD.operation";
import pagination from "@crud/Pagination";
import { get } from "@/api/lab";

const defaultForm = {
  id: null,
  // name: null,
  type: null,
  // labId: null,
  isForbidden: 0,
  // thresholdValue: null,
  createdAt: null,
  updatedAt: null,
  isDeleted: null,
  data: null,
  slaveStationId: null,
  // ip: null,
  // address: null,
};
export default {
  name: "Sensor",
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: "传感器",
      url: "api/sensor/querySensorByType",
      idField: "id",
      sort: "id,desc",
      crudMethod: { ...crudSensor },
      // query: {
      //   type:'1,2,3',
      // }
    });
  },
  data() {
    return {
      permission: {
        add: ["admin", "sensor:add"],
        edit: ["admin", "sensor:edit"],
        del: ["admin", "sensor:del"],
      },
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        type: [
          { required: true, message: "传感器类型不能为空", trigger: "blur" },
        ],
        // slaveStationId: [
        //   { required: true, message: '从站地址不能为空', trigger: 'blur' }
        // ],
        labId: [
          { required: true, message: "实验室不能为空", trigger: "change" },
        ],
        // isForbidden: [
        //   {
        //     required: true,
        //     message: "是否禁用短信报警不能为空",
        //     trigger: "blur"
        //   }
        // ],
        thresholdValue: [
          { required: true, message: "阈值不能为空", trigger: "blur" },
        ],
        ip: [{ required: true, message: "ip不能为空", trigger: "blur" }],
        address: [{ required: true, message: "地址不能为空", trigger: "blur" }],
      },
      queryTypeOptions: [
        { key: "name", display_name: "名称" },
        { key: "type", display_name: "传感器类型" },
        { key: "labId", display_name: "实验室" },
        {
          key: "isForbidden",
          display_name: "是否禁用短信报警：1是，0否，默认0",
        },
        { key: "thresholdValue", display_name: "阈值" },
      ],
      marks: {
        0: "0",
        100: {
          style: { right: "-20%" },
          label: "100%",
        },
        50: {
          style: {
            color: "#1989FA",
          },
          label: this.$createElement("strong", "50%"),
        },
      },
      marks2: {
        0: "0",
        100: {
          style: { right: "-20%" },
          label: "100°C",
        },
        50: {
          style: {
            color: "#1989FA",
          },
          label: this.$createElement("strong", "50°C"),
        },
      },
      marks3: {
        0: "0",
        50: {
          style: { right: "-20%" },
          label: "50Pa",
        },
        25: {
          style: {
            color: "#1989FA",
          },
          label: this.$createElement("strong", "25Pa"),
        },
      },
      labs: [],
    };
  },
  created() {
    this.getLabs();
  },
  methods: {
    getLabs() {
      get()
        .then((response) => {
          this.labs = response;
          // console.log(response)
        })
        .catch((_) => {
          // console.log(response);
        });
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true;
    },
  },
};
</script>

<style scoped></style>
